<template>
  <div class="view-container">
    <el-form :model="queryParam" label-width="auto">
      <el-row :gutter="10">
        <el-col :span="6">
          <el-form-item label="部门名称" prop="deptName">
            <el-input v-model="queryParam.deptName" placeholder="请输入部门名称"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="部门状态" prop="deptStatus">
            <el-select v-model="queryParam.deptStatus" class="width-control" placeholder="请选择部门状态"
                       :allow-clear="true">
              <el-option :value=0 label="正常"></el-option>
              <el-option :value=1 label="停用"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item>
            <el-button type="primary" @click="handleQuery">搜索</el-button>
            <el-button @click="resetParams">重置</el-button>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <nebula-table :columns="columns" :data="tableData" use-tool-bar border
                  :tree-props="{ children: 'childList' }" row-key="id" @change="changePage"
                  header-row-class-name="header-row-class" :default-page-size="10" show-overflow-tooltip :total="rowsTotal">
      <template #head-toolbar>
        <el-button type="primary" @click="handleCreate" size="small">新增</el-button>
      </template>
      <template #deptName="scope">
        <router-link :to=genDetailUrl(scope.row) class="table-link">
          {{ scope.row.deptName }}
        </router-link>
      </template>
      <template #deptStatus="scope">
        <el-switch v-model="scope.row.deptStatus" :active-value="0" :inactive-value="1" active-text="正常"
                   inactive-text="停用" @change="switchChange(scope)" inline-prompt></el-switch>
      </template>
      <template #operation="scope">
        <el-button type="primary" size="small" @click="handleEdit(scope.row)">编辑</el-button>
        <el-button type="danger" size="small" @click="handleDelete(scope.row)">删除</el-button>
      </template>
    </nebula-table>
    <el-dialog v-model="isShow" title="新增部门">
      <div class="view-container">
        <el-form :model="createParam" label-width="auto">
          <el-row :gutter=20>
            <el-col :span="12" sm="24">
              <el-form-item label="父级菜单" prop="parentId">
                <el-tree-select v-model="createParam.parentId" :data="tableData"
                                :replaceFields="replaceFields" class="width-control"
                                allow-clear></el-tree-select>
              </el-form-item>
            </el-col>
            <el-col :span="12" sm="24">
              <el-form-item label="部门名称" prop="deptName">
                <el-input v-model="createParam.deptName"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12" sm="24">
              <el-form-item label="部门领导" prop="leader">
                <el-select v-model="createParam.leader" :show-search="true" class="width-control"
                           allow-clear>
                  <el-option v-for="item in userInfo" :value="item.id"
                             :label="item.nickname"></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="12" sm="24">
              <el-form-item label="排序" prop="orderNo">
                <el-input-number v-model="createParam.orderNo" :min="0"
                                 class="width-control"></el-input-number>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
      </div>
      <template #footer>
        <el-button @click="createCancel">取消</el-button>
        <el-button type="primary" @click="createSubmit">确定</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import {onMounted} from "vue";
import {changeDeptStatus, createOrUpdateDept, getTableData} from "@/api/sysDept.js";
import {getNormalUser} from "@/api/sysUser.js";
import NebulaTable from "@/components/NebulaTable.vue";
import {ElMessageBox, ElNotification} from "element-plus";

defineOptions({
  name: 'sysDept'
})

const queryParam = ref({
  deptName: null,
  deptStatus: null,
  pageNum: 1,
  pageSize: 10
})

const columns = [
  {title: "部门名称", key: "deptName", customSlot: "deptName"},
  {title: "管理人", key: "leaderName", align: 'center'},
  {title: "排序", key: "orderNo", align: 'center'},
  {title: "部门状态", key: "deptStatus", align: 'center', customSlot: 'deptStatus'},
  {title: "创建时间", key: "createTime", align: 'center'},
  {title: "操作", align: 'center', customSlot: 'operation'}
]

const rowsTotal = ref(0)
const tableData = ref([])

const isShow = ref(false)
const createParam = ref({
  deptName: null,
  parentId: null,
  leader: null,
  orderNo: 1,
})
const replaceFields = ref({
  title: "deptName",
  children: "childList"

})

const userInfo = ref([])

const createSubmit = () => {
  createOrUpdateDept(createParam.value).then(res => {
    if (res.code === 200) {
      isShow.value = false
      handleQuery()
      ElNotification({
        title: "操作成功",
        type: "success"
      })
    } else {
      ElNotification({
        title: "操作失败",
        type: "error",
        message: res.msg
      })
    }
  })
}

const createCancel = () => {
  isShow.value = false
  for (let key in createParam.value) {
    if (key === "orderNo") {
      createParam.value[key] = 1
    } else {
      createParam.value[key] = null;
    }
  }
}

const genDetailUrl = (row) => {
  return "/system/dept/detail/" + row.id;
}

const changePage = (currentPage,pageSize) => {
  queryParam.value.pageNum = currentPage
  queryParam.value.pageSize = pageSize
  handleQuery()
}

const handleQuery = () => {
  getTableData(queryParam.value).then(res => {
    tableData.value = res.data.rows
    rowsTotal.value = res.data.total
  })
}

const resetParams = () => {
  for (let key in queryParam.value) {
    if (key !== "pageNum" && key !== "pageSize") {
      queryParam.value[key] = null;
    }
  }
}

const switchChange = (scope) => {
  const row = scope.row
  const index = scope.index
  if (index === -1) return

  const changeParam = {
    id: row.id,
    deptStatus: row.deptStatus
  }
  changeDeptStatus(changeParam).then(res => {
    if (res.code === 200) {
      handleQuery()
      ElNotification({
        title: "操作成功",
        type: "success"
      })
    } else {
      ElNotification({
        title: "创建失败",
        message: res.msg,
        type: "error"
      })
    }
  })
}

const handleCreate = () => {
  isShow.value = true
}

const getUserInfo = () => {
  getNormalUser().then(res => {
    if (res.code === 200) userInfo.value = res.data
  })
}

const handleEdit = (row) => {
  createParam.value = {...row}
  handleCreate()
}

const handleDelete = (row) => {
  const confirmText = `确定要删除部门【${row.deptName}】及其下属部门吗?`
  ElMessageBox.confirm(
      confirmText,
      {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      }
  ).then(() => {
    const changeParam = {
      id: row.id,
      isDelete: 1
    }
    changeDeptStatus(changeParam).then(res => {
      if (res.code === 200) {
        handleQuery()
        ElNotification({
          title: "操作成功",
          type: "success"
        })
      } else {
        ElNotification({
          title: "创建失败",
          message: res.msg,
          type: "error"
        })
      }
    })
  })
}


onMounted(() => {
  getUserInfo()
  handleQuery()
})

</script>
